package dao.impl;


import dao.UserDao;
import domain.User;
import org.apache.commons.dbutils.QueryRunner;
import util.DataSourceUtils;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public boolean insert(User user) {
        String sql = "insert into user(username, password, gender, email, telephone, introduce) values(?, ?, ?, ?, ?, ?)";
        Connection connection = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getGender());
            statement.setString(4, user.getEmail());
            statement.setString(5, user.getTelephone());
            statement.setString(6, user.getIntroduce());
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(connection, statement, null);
        }
        return result != 0;
    }

    @Override
    public User findUser(String username, String password) {
        String sql="select * from user where username = ? and password = ?";
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            user = JDBCUtil.parseResultSet(resultSet, User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(connection, statement, resultSet);
        }
        return user;
    }

    @Override
    public User findName(String username) throws SQLException {
        String sql="select * from user where username = ?";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.query(sql, rs -> JDBCUtil.parseResultSet(rs, User.class), username);
    }

    @Override
    public boolean editUser(String username, String rePassword, String telephone) {
        String sql = "update user set password = ?, telephone = ? where username = ?";
        Connection connection = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1, rePassword);
            statement.setString(2, telephone);
            statement.setString(3, username);
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            JDBCUtil.closeAll(connection, statement, null);
        }
        return result != 0;
    }

    @Override
    public List<User> userList(int page, int pageSize) throws SQLException {
        String sql = "select * from user limit " + page * pageSize + ", " + pageSize;
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.query(sql, rs -> {
            List<User> userList = new ArrayList<>();
            User user;
            while ((user = JDBCUtil.parseResultSet(rs, User.class)) != null) {
                userList.add(user);
            }
            return userList;
        });
    }
}
